#coding=utf-8
#LightCam 熔断视图
#init by hfh 20151113

#20141209,084829,trade,01,MAXZHITradeCnt,交易系统,股指成交峰值,3444270,,,

import multiprocessing,subprocess,sys
import socket,os,time,re,traceback,json,binascii
import lc_libs,pymongo


def genjson(filename,data):
	jdata={}
	jdata['rows']=data
	file=open(lchome+"/pyweb/static/json/breaker/"+filename+".json",'w')
	file.write(json.dumps(jdata))
	file.close()


if __name__ == "__main__":
	
	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()


	#初始化数据库
	
	db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

	db.connect()
	Conn=db.getConn()
	if Conn==None:
		print "Can not Connect to the MondbDB"
		exit(0)
	
	dbname='lc_breaker'
	psisdb=db.useDatabase(dbname)
	
	while True:
		try:
			
			#测试用 start
			#thisfile=open("/monitor/LightCam/temp/process.json","rb")
			#datastr1=thisfile.read()
			#thisfile.close()
			#jsondata=json.loads(datastr1)
			#genjson('process',jsondata)
			#
			#thisfile=open("/monitor/LightCam/temp/events.json","rb")
			#datastr1=thisfile.read()
			#thisfile.close()
			#jsondata=json.loads(datastr1)
			#genjson('events',jsondata)
			#
			#thisfile=open("/monitor/LightCam/temp/quote.json","rb")
			#datastr1=thisfile.read()
			#thisfile.close()
			#jsondata=json.loads(datastr1)
			#genjson('quote',jsondata)
			#
			#thisfile=open("/monitor/LightCam/temp/crond.json","rb")
			#datastr1=thisfile.read()
			#thisfile.close()
			#jsondata=json.loads(datastr1)
			#genjson('crond',jsondata)
			#
			#thisfile=open("/monitor/LightCam/temp/appstatus.json","rb")
			#datastr1=thisfile.read()
			#thisfile.close()
			#jsondata=json.loads(datastr1)
			#genjson('appstatus',jsondata)
			#
			#exit(0)
			#测试用 end

			cmds='''
				cd /monitor/LightCam/scripts/;
				LightCamPy lc_batch.py "fuse1|fuse2" getsp /monitor/LightCam/temp/quote.json quote.json /monitor/LightCam/temp/;
				LightCamPy lc_batch.py "fuse1|fuse2" getsp /monitor/LightCam/temp/events.json events.json /monitor/LightCam/temp/;
				LightCamPy lc_batch.py "fuse1|fuse2" getsp /monitor/LightCam/temp/crond.json crond.json /monitor/LightCam/temp/;
				LightCamPy lc_batch.py "fuse1|fuse2" getsp /monitor/LightCam/temp/process.json process.json /monitor/LightCam/temp/;
				LightCamPy lc_batch.py "ezsr1|ezsr2" getsp /d/monitor/LightCam/temp/process.json process.json /monitor/LightCam/temp/;
				LightCamPy lc_batch.py "fuse1|fuse2" getsp /monitor/LightCam/temp/appstatus.json appstatus.json /monitor/LightCam/temp/
			'''
			
			#cmds="id"
			os.system(cmds)

			#进程状态统计
			thisfile=open("/monitor/LightCam/temp/fuse1_process.json","rb")
			datastr1=thisfile.read()
			thisfile.close()
			thisfile=open("/monitor/LightCam/temp/fuse2_process.json","rb")
			datastr2=thisfile.read()
			thisfile.close()
			thisfile=open("/monitor/LightCam/temp/ezsr1_process.json","rb")
			datastr3=thisfile.read()
			thisfile.close()
			thisfile=open("/monitor/LightCam/temp/ezsr2_process.json","rb")
			datastr4=thisfile.read()
			thisfile.close()
			jsondata1=json.loads(datastr1)
			jsondata2=json.loads(datastr2)
			jsondata3=json.loads(datastr3)
			jsondata4=json.loads(datastr4)
			#jsondata=jsondata1+jsondata2
			jsondata=jsondata1+jsondata2+jsondata3+jsondata4
			genjson('process',jsondata)

			#日志统计
			thisfile=open("/monitor/LightCam/temp/fuse1_events.json","rb")
			datastr1=thisfile.read()
			thisfile.close()
			thisfile=open("/monitor/LightCam/temp/fuse2_events.json","rb")
			datastr2=thisfile.read()
			thisfile.close()
			jsondata1=json.loads(datastr1)
			jsondata2=json.loads(datastr2)
			jsondata=jsondata1+jsondata2
			genjson('events',jsondata)

			#行情统计
			thisfile=open("/monitor/LightCam/temp/fuse1_quote.json","rb")
			datastr1=thisfile.read()
			thisfile.close()
			#thisfile=open("/monitor/LightCam/temp/fuse2_quote.json","rb")
			#datastr2=thisfile.read()
			#thisfile.close()
			jsondata1=json.loads(datastr1)
			#jsondata2=json.loads(datastr2)
			#jsondata=jsondata1+jsondata2
			jsondata=jsondata1
			genjson('quote',jsondata)

			#熔断进度
			thisfile=open("/monitor/LightCam/temp/fuse1_crond.json","rb")
			datastr1=thisfile.read()
			thisfile.close()
			#thisfile=open("/monitor/LightCam/temp/fuse2_crond.json","rb")
			#datastr2=thisfile.read()
			#thisfile.close()
			jsondata1=json.loads(datastr1)
			#jsondata2=json.loads(datastr2)
			#jsondata=jsondata1+jsondata2
			jsondata=jsondata1
			genjson('crond',jsondata)

			#应用状态
			thisfile=open("/monitor/LightCam/temp/fuse1_appstatus.json","rb")
			datastr1=thisfile.read()
			thisfile.close()
			#thisfile=open("/monitor/LightCam/temp/fuse2_appstatus.json","rb")
			#datastr2=thisfile.read()
			#thisfile.close()
			jsondata1=json.loads(datastr1)
			#jsondata2=json.loads(datastr2)
			#jsondata=jsondata1+jsondata2
			jsondata=jsondata1
			genjson('appstatus',jsondata)

			time.sleep(10)

		except Exception:
			excinfo=traceback.format_exc()
			logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			logfile.write(logdate+" \n"+excinfo+"\n")
			logfile.flush()
			time.sleep(10)
			continue

